<template>
	<view class="main" :style="{ '--color': vuex_config.themeColor1 }">
		<sww-nav-bar v-if="navInit" title="我的" :background="navObj.bgColor || 'transparent'" :titleColor="navObj.fColor || '#000000'"></sww-nav-bar>
		<view>
			<template v-for="item of templateList">
				<template v-if="isShow(item.parameter.showComponent, item.code)">
					<view
						class="com_item"
						:key="item.dispose_id"
						:style="{
							margin: (item.parameter.marginTop || 0) + 'px ' + (item.parameter.marginLR || 0) + 'px ' + ' 0px',
							background: item.parameter.bgColor || 'transparent',
							padding: (item.parameter.padTop || 0) + 'px' + ' ' + (item.parameter.padLR || 0) + 'px ' + (item.parameter.padBot || 0) + 'px',
							'border-radius':
								(item.parameter.radiusTop || 0) +
								'px ' +
								(item.parameter.radiusTop || 0) +
								'px ' +
								(item.parameter.radiusBot || 0) +
								'px ' +
								(item.parameter.radiusBot || 0) +
								'px',
							overflow: item.code != 'userInfo' && item.code !== 'listCategory' ? 'hidden' : ''
						}"
					>
						<component @groupItemGo="groupItemGo" :templateItem="item" is="rowOpen" v-if="item.code == 'rowOpen'"></component>
						<component @groupItemGo="groupItemGo" :templateItem="item" is="singleRow" v-else-if="item.code == 'singleRow'"></component>
						<component @groupItemGo="groupItemGo" :templateItem="item" is="tab" v-else-if="item.code == 'tab'"></component>
						<component @groupItemGo="groupItemGo" :templateItem="item" is="shareApply" v-else-if="item.code == 'shareApply'"></component>
						<component @groupItemGo="groupItemGo" :templateItem="item" is="userHeader" v-else-if="item.code == 'userHeader'"></component>
						<component @groupItemGo="groupItemGo" :templateItem="item" is="moduleItems" v-else-if="item.code == 'moduleItems'"></component>
						<component :templateItem="item" is="auctionVip" v-else-if="item.code == 'auctionVip'"></component>
					</view>
				</template>
			</template>
		</view>
		<sww-login></sww-login>
		<view class="bottom"></view>
	</view>
</template>
<script>
import moduleItems from '@/template/moduleItems.vue';
import userHeader from './components/userHeader.vue';
import singleRow from './components/singleRow.vue';
import tab from './components/tab.vue';
import shareApply from './components/shareApply.vue';
import rowOpen from './components/rowOpen.vue';
import auctionVip from './components/auctionVip.vue';
import { pageShare } from '../../mixins/pageShare';
import { uniLinkTo, uniModal, uniCall,openAddress } from '@/utils/uniPromise.js';
import { mapState, mapActions } from 'vuex';
import { buildUrl } from '@/utils/index.js';
import { pageComponentsLink } from '@/utils/jump.js';
import { wxConfig } from '@/mixins/wxConfig';
export default {
	mixins: [pageShare,wxConfig],
	data() {
		return {
			navInit:false,
			navObj: {},
			templateList: [],
			urlArr: [
				{ linkUrl: 'order_notpayment', title: '待支付订单', path: '/package/order/list/index', query: { id: 1 } },
				{ linkUrl: 'order_consignment', title: '已支付订单', path: '/package/order/list/index', query: { id: 2 } },
				{ linkUrl: 'order_take', title: '待收货订单', path: '/package/order/list/index', query: { id: 3 } },
				{ linkUrl: 'order_complete', title: '待评价订单', path: '/package/order/list/index', query: { id: 4 } },
				{ linkUrl: 'order_refund', title: '退款中订单', path: '/package/order/list/index', query: { id: 5 } },
				{ linkUrl: 'extension', title: '推广二维码', path: '/package/user/code/index' },
				{ linkUrl: 'shareOrder', title: '分享订单', path: '/package/distribution/orderList/index' },
				{ linkUrl: 'shareGuide', title: '分享指引', path: '/package/user/guide/index?type=guide' },
				{ linkUrl: 'myShop', title: '我的小店', path: '/package/user/my-shop/shop/index' },
				{ linkUrl: 'regionalOrder', title: '区域订单', path: '/package/user/regionalOrder/index' },
				{ linkUrl: 'coupon', title: '优惠劵', path: '/package/user/coupon/index' },
				{ linkUrl: 'group', title: '特惠拼团', path: '/package/product/group/index' },
				{ linkUrl: 'integralEx', title: '积分兑换', path: '/package/integral/index' },
				{ linkUrl: 'myCard', title: '我的名片', path: '/package/business-card/index' },
				{ linkUrl: 'address', title: '收货地址', method: 'goAddress' },
				{ linkUrl: 'admin', title: '管理端', path: '/package/system/login' },
				{ linkUrl: 'abouts', title: '关于我们', path: '/package/user/guide/index' },
				{ linkUrl: 'workers', title: '职工信息', path: '/package/user/user-members/index' },
				{ linkUrl: 'pool', title: '选品池', path: '/package/business-card/selectPool/index' },
				{ linkUrl: 'teamManage', title: '团队管理', method:'goUserTean' },
				{ linkUrl: 'bond', title: '保证金', path:'/package/user/deposit/index' },
				{ linkUrl: 'comLinkUrl', title: '通用事件', method:'comLinkUrljump' },
				{ linkUrl: 'envelope', title: '红包', path:'/package/redEnvelopes/index' },
				{ linkUrl: 'application', title: '申请团长', path:'/package/userSuper/pickUp/headoapplication/index' },
				{ linkUrl: 'writeofforder', title: '核销订单', path:'/package/userSuper/pickUp/writeofforder/index' },
				{ linkUrl: 'pickuporder', title: '自提订单', path:'/package/userSuper/pickUp/pickuporder/index' },
				{ linkUrl: 'conciergeCard', title: '我的礼宾卡', path:'/package/common/conciergeCard/list' },
				{ linkUrl: 'guest', title: '我的客人', path: '/package/userSuper/share/guest/index' },
				{ linkUrl: 'shareRank', title: '分享排行榜', path: '/package/userSuper/share/rank/index' },
				{ linkUrl: 'profit', title: '消费返现', path: '/package/userSuper/share/withdMerge/index' },
				{ linkUrl: 'bonus', title: '奖金提现', path: '/package/userSuper/share/distribution/withdrawal/index' },
				{ linkUrl: 'rakeBack', title: '返佣明细', path:'/package/userSuper/share/distribution/detailList/index' },
				{ linkUrl: 'bonusAbout', title: '分红奖金', path: '/package/userSuper/share/bonus/withdrawal/index' },
				{ linkUrl: 'shareVal', title: '分红明细', path: '/package/userSuper/share/bonus/detailList/index' },
				{ linkUrl: 'profitRank', title: '收益排行榜', path:'/package/userSuper/share/bonus/prizePool/index' },
			]
		};
	},
	components: {
		moduleItems,
		userHeader,
		singleRow,
		tab,
		shareApply,
		rowOpen,
		auctionVip,
	},
	computed: {
		...mapState(['vuex_user']),
		isShow(type, code) {
			return function(type, code) {
				if (type == 2) {
					if (this.vuex_user.type == 2 || this.vuex_user.type == 4) {
						return true;
					} else {
						return false;
					}
				} else if (code == 'shareApply') {
					return !this.vuex_user.pk_id || this.vuex_user.type == 1 || this.vuex_user.type == 3;
				} else {
					return true;
				}
			};
		}
	},
	methods: {
		// 查询用户数据、余额
		...mapActions(['loadUserCount','loadUserInfo']),
		$onLoad() {
			// #ifdef H5
			// h5需要先获取支付和地址的配置
			this.initWxConfig(['openAddress'])
				.then(() => {})
				.catch(() => {});
			// #endif
			this.$login();
			this.initTemplateList();
		},
		$onShow() {
			this.shareOnshow()
			if (this.$isLogin()) {
				this.loadUserCount();
				this.loadUserInfo().then(data => {
					let historyType = uni.getStorageSync('applyDistributorKey');
					if (historyType == 3) {
						if (data.type == '2') {
							uniModal({
								content: '恭喜您成为成为好物推荐官',
								showCancel: false
							});

							uni.setStorageSync('applyDistributorKey', 2);
						} else if (data.type == '1') {
							uniModal({
								content: '商家拒绝您成为好物推荐官',
								showCancel: false
							});

							uni.setStorageSync('applyDistributorKey', 0);
						}
					}
					this.$u.vuex('vuex_user.type', data.type);
					this.$u.vuex('vuex_user.level_name', data.level_name);
				});
			}
		},
		$onPullDownRefresh() {
			this.initTemplateList();
		},
		initTemplateList() {
			this.templateList = [];
			this.$loading();
			if (!this.vuex_business.pk_id) {
				setTimeout(() => {
					this.initTemplateList();
				}, 300);
				return;
			}
			this.$resPost('GetAssemblyDispose', {
				type: 2
			})
				.then(({ data }) => {
					let modules = data[0] ? data : this.temArr;
					if (modules[0].code === 'userNav') {
						this.navObj = JSON.parse(modules[0].dispose_parameter || '{}');
						modules.splice(1, 0);
					}else{
						this.navObj = {}
					}
					this.navInit = true
					this.templateList = modules.map(item => {
						let obj = { ...item };
						if (obj.dispose_parameter) {
							try {
								obj.parameter = JSON.parse(obj.dispose_parameter);
							} catch (e) {
								obj.parameter = {};
							}
						} else {
							obj.parameter = {};
						}
						delete obj.dispose_parameter;
						if (obj.dispose_pagedata) {
							try {
								obj.pageData = JSON.parse(obj.dispose_pagedata);
							} catch (e) {
								obj.pageData = [];
							}
						} else {
							obj.pageData = [];
						}
						delete obj.dispose_pagedata;
						if (obj.dispose_apiparams) {
							try {
								obj.apiParams = JSON.parse(obj.dispose_apiparams);
							} catch (e) {
								obj.apiParams = [];
							}
						} else {
							obj.apiParams = [];
						}
						delete obj.dispose_apiparams;
						return obj;
					});
					this.$hide();
				})
				.catch(() => {
					this.navInit = true
					this.$hide();
				});
		},
		// 跳转到订单
		goOrder(index) {
			let url = `/package/order/list/index?id=${index}`;
			if (index <= 0 && this.showOldOrder) {
				url = '/package/order/oldOrderList/index';
			}
			this.$login('goOrder', index).then(() => {
				uniLinkTo(url);
			});
		},

		// 点击菜单
		groupItemGo(item) {
			if (item.linkUrl == 'concat') {
				// #ifdef H5
				let phone = this.vuex_business.custom_tel;
				uniCall(phone);
				// #endif
			} else {
				this.$login('groupItemGo', item).then(() => {
					let path, query;
					let obj = this.urlArr.find(urlItem => {
						if (urlItem.linkUrl == item.linkUrl) {
							return true;
						} else {
							return false;
						}
					});
					if (!obj) {
						// uniModal({
						// 	content: '功能未实现',
						// 	showCancel: false
						// });
					} else if (obj.method) {
						this[obj.method](item);
					} else if (obj.path) {
						path = obj.path;
						query = {
							...(obj.query || {})
						};
						if (item.query) {
							query = {
								...query,
								...item.query
							}
						}
						let url = buildUrl(path, query);
						this.$linkTo(url);
					} else {
						uniModal({
							content: '功能未实现',
							showCancel: false
						});
					}
				});
			}
		},
		// 收货地址
		goAddress() {
			openAddress()
		},
		// 团队管理 vuex_user.is_agent == 1
		goUserTean () {
			if(this.vuex_user.is_agent == 1){
				this.$linkTo('/package/user/my-shop/teamPerformance/index')
			}else{
				uni.showModal({
					content:'您并未加入团队',
					showCancel:false
				})
			}
		},
		comLinkUrljump (item) {
			let obj = {
				...item,
				linkUrl:item.comLink
			}
			pageComponentsLink(obj);
		}
	}
};
</script>
<style lang="scss" scoped>
.main {
	background-color: #F5F5F5;
	min-height: 100vh;
}
</style>
